package com.aizuda.boot.modules.business.permission.controller;

import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.business.permission.domain.request.AddRoleVO;
import com.aizuda.boot.modules.business.permission.domain.request.PageRoleVO;
import com.aizuda.boot.modules.business.permission.domain.request.QueryRoleListVo;
import com.aizuda.boot.modules.business.permission.domain.request.UpdateRoleVO;
import com.aizuda.boot.modules.business.permission.domain.response.OwnPermissionVO;
import com.aizuda.boot.modules.business.permission.domain.response.PageRoleResponseVO;
import com.aizuda.boot.modules.business.permission.domain.response.RoleBasicVO;
import com.aizuda.boot.modules.business.permission.domain.response.RoleShowVO;
import com.aizuda.boot.modules.business.permission.provider.RoleProvider;
import com.aizuda.boot.modules.system.entity.SysRole;
import com.aizuda.boot.modules.system.service.ISysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author duanyao
 * @date 2022/12/05 15:07
 * @description: 角色单表
 */
@Tag(name = "角色管理")
@RequestMapping("/role")
@RestController
@Validated
@Slf4j
public class RoleController {

    @Autowired
    private RoleProvider roleProvider;

    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 修改角色权限状态
     *
     * @param roleId
     * @return
     */
    @Operation(summary = "修改角色权限状态_新")
    @PutMapping("/enable/{roleId}")
    public SingleResponse<Void> enableRole(@PathVariable(value = "roleId") Long roleId) {
        SysRole role = sysRoleService.getById(roleId);
        sysRoleService.updateStatusById(roleId, role.getStatus().equals(1) ? 0 : 1);
        return SingleResponse.success();
    }

    /**
     * 获取角色权限信息
     *
     * @param roleId
     * @return
     */
    @Operation(summary = "获取角色权限信息_新")
    @GetMapping("/get/{roleId}")
    public SingleResponse<RoleShowVO> getRole(@PathVariable("roleId") Long roleId) {
        return roleProvider.getRole(roleId);
    }

    /**
     * 添加角色信息
     *
     * @param addRoleVO
     * @return
     */
    @Operation(summary = "添加角色信息_新")
    @PostMapping("/add")
    public SingleResponse<Void> addRole(@RequestBody AddRoleVO addRoleVO) {
        return roleProvider.addRole(addRoleVO);
    }

    /**
     * 修改角色信息
     *
     * @param updateRoleVO
     * @return
     */
    @Operation(summary = "修改角色信息_新")
    @PutMapping("/update")
    public SingleResponse<Void> updateRole(@RequestBody UpdateRoleVO updateRoleVO) {
        return roleProvider.updateRole(updateRoleVO);
    }

    /**
     * 分页查询角色信息
     *
     * @param pageNo
     * @param pageSize
     * @param pageRoleVO
     * @return
     */
    @Operation(summary = "分页查询角色信息_新")
    @PostMapping("/page/{pageNo}/{pageSize}")
    public SingleResponse<IPage<PageRoleResponseVO>> pageRole(@PathVariable("pageNo") Integer pageNo,
                                              @PathVariable("pageSize") Integer pageSize,
                                              @RequestBody(required = false) PageRoleVO pageRoleVO) {
        IPage<PageRoleResponseVO> page = new Page<>(pageNo, pageSize);
        return roleProvider.pageRole(page, pageRoleVO);
    }

    @Operation(summary = "删除角色_新")
    @DeleteMapping("/delete/{roleId}")
    public SingleResponse<Void> deleteRole(@PathVariable Long roleId) {
        return roleProvider.deleteRole(roleId);
    }

    @Operation(summary = "获取全部角色列表")
    @PostMapping("/getRoleList")
    public SingleResponse<List<Map<String, String>>> getRoleListByType() {
        QueryRoleListVo queryRoleListVo = new QueryRoleListVo();
        return roleProvider.getRoleListByType(queryRoleListVo);
    }

    @Operation(summary = "根据角色ID获取角色基本信息(非前端调用，后端feign调用)")
    @GetMapping("/basic/{roleId}")
    public SingleResponse<RoleBasicVO> getRoleBasicInfo(@PathVariable("roleId") Long roleId) {
        return roleProvider.getRoleBasicInfo(roleId);
    }


    @Operation(summary = "获取当前登录用户权限")
    @GetMapping("/current")
    public SingleResponse<OwnPermissionVO> getCurrentRole() {
        return roleProvider.getCurrentPermission();
    }
}
